#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Key.h"
#include "Buzzer.h"
#include "Servo.h"
#include "LightSensor.h"
#include "Encoder.h"
#include "Motor.h"
#include "Serial.h"
#include "DHT11.h"
#include "DS1302.h"
#include "TIM3.h"

uint8_t KeyNum, MODE, TimeSetSelect;
float Servo_Angle = 0;;
uint16_t Num;
int8_t Speed = 100;
uint8_t DHT11_Temp_Int, DHT11_Temp_Flo, DHT11_Humi_Int, DHT11_Humi_Flo;

int main(void)
{
	Serial_Init();
	Encoder_Init();
	OLED_Init();
	Key_Init();
	Buzzer_Init();
	Servo_Init();
	LightSensor_Init();
	Motor_Init();
	DHT11_Init();
	DS1302_Init();
	Timer_Init();
	
	DS1302_SetTime();
	Servo_SetAngle(0);
	OLED_ShowString(1, 1, "Alarm:xx:xx:xx");
	OLED_ShowString(2, 1, "Date:xx-xx-xx-Wx");
	OLED_ShowString(3, 1, "Time:xx:xx:xx");
	OLED_ShowString(4, 1, "H:xx% T:xx.xC");

	while(1)
	{

		DHT11_Read_Data(&DHT11_Humi_Int, &DHT11_Temp_Int, &DHT11_Temp_Flo);
		OLED_ShowNum(4, 3, DHT11_Humi_Int, 2);
		OLED_ShowNum(4, 9, DHT11_Temp_Int, 2);
		OLED_ShowNum(4, 12, DHT11_Temp_Flo, 1);
		if(DHT11_Temp_Int >= 30)Speed = 0;
		else Speed = 100;

		KeyNum = Key_GetNum();
		if(KeyNum == 4)
		{
			if(MODE == 0)//当前show模式
			{
				MODE = 1;
				TimeSetSelect = 0;//show模式切回set模式后初始年闪烁	
			}
			else//当前DS1302修改模式
			{
				MODE = 0;
				DS1302_SetTime();//DS1302模式修改后切闹钟修改模式要保存修改值
			}
		}
		if(MODE)
		{
			if(KeyNum == 3)
			{
				TimeSetSelect++;
				TimeSetSelect%=10;//0-9循环
			}
			
			DS1302_Time[TimeSetSelect] += Encoder_Get();//通过旋转编码器修改时间和闹钟
//优化修改时间的范围	
			if(DS1302_Time[0]>99){DS1302_Time[0]=1;}
			if(DS1302_Time[1]>12){DS1302_Time[1]=1;}
			if(DS1302_Time[3]>23){DS1302_Time[3]=0;}
			if(DS1302_Time[4]>59){DS1302_Time[4]=0;}
			if(DS1302_Time[5]>59){DS1302_Time[5]=0;}
			if(DS1302_Time[6]>7){DS1302_Time[6]=1;}		
			
			if(DS1302_Time[0]<1){DS1302_Time[0]=99;}//调试BUG：01--后是99，99++后是00
			if(DS1302_Time[1]<1){DS1302_Time[1]=12;}
			if(DS1302_Time[3]<0){DS1302_Time[3]=23;}
			if(DS1302_Time[4]<0){DS1302_Time[4]=59;}
			if(DS1302_Time[5]<0){DS1302_Time[5]=59;}
			if(DS1302_Time[6]<1){DS1302_Time[6]=7;}
			
			if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==5||DS1302_Time[1]==7||DS1302_Time[1]==8||DS1302_Time[1]==10||DS1302_Time[1]==12)//单月
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=31;}
				if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
			}
			else if(DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==9||DS1302_Time[1]==11)//双月
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
				if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
			}
			else if(DS1302_Time[1]==2)
			{
				if(DS1302_Time[0]%4==0)//闰年
				{
					if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
					if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
				}
				else
				{
					if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
					if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
				}
			}
			
//优化修改闹钟的范围	
			if(DS1302_Time[7]>23){DS1302_Time[7]=0;}
			if(DS1302_Time[8]>59){DS1302_Time[8]=0;}
			if(DS1302_Time[9]>59){DS1302_Time[9]=0;}
			if(DS1302_Time[7]<0){DS1302_Time[7]=23;}
			if(DS1302_Time[8]<0){DS1302_Time[8]=59;}
			if(DS1302_Time[9]<0){DS1302_Time[9]=59;}
			
//修改模式下OLED闪烁
			if(TimeSetSelect==0&&TimeSetFlashFlag==1){OLED_ShowString(2, 6, "  ");}
			else{OLED_ShowNum(2, 6, DS1302_Time[0], 2);}
			if(TimeSetSelect==1&&TimeSetFlashFlag==1){OLED_ShowString(2, 9, "  ");}
			else{OLED_ShowNum(2, 9, DS1302_Time[1], 2);}
			if(TimeSetSelect==2&&TimeSetFlashFlag==1){OLED_ShowString(2, 12, "  ");}
			else{OLED_ShowNum(2, 12, DS1302_Time[2], 2);}
			if(TimeSetSelect==3&&TimeSetFlashFlag==1){OLED_ShowString(3, 6, "  ");}
			else{OLED_ShowNum(3, 6, DS1302_Time[3], 2);}
			if(TimeSetSelect==4&&TimeSetFlashFlag==1){OLED_ShowString(3, 9, "  ");}
			else{OLED_ShowNum(3, 9, DS1302_Time[4], 2);}
			if(TimeSetSelect==5&&TimeSetFlashFlag==1){OLED_ShowString(3, 12, "  ");}
			else{OLED_ShowNum(3, 12, DS1302_Time[5], 2);}
			if(TimeSetSelect==6&&TimeSetFlashFlag==1){OLED_ShowString(2, 16, " ");}
			else{OLED_ShowNum(2, 16, DS1302_Time[6], 1);}
			if(TimeSetSelect==7&&TimeSetFlashFlag==1){OLED_ShowString(1, 7, "  ");}
			else{OLED_ShowNum(1, 7, DS1302_Time[7], 2);}
			if(TimeSetSelect==8&&TimeSetFlashFlag==1){OLED_ShowString(1, 10, "  ");}
			else{OLED_ShowNum(1, 10, DS1302_Time[8], 2);}
			if(TimeSetSelect==9&&TimeSetFlashFlag==1){OLED_ShowString(1, 13, "  ");}
			else{OLED_ShowNum(1, 13, DS1302_Time[9], 2);}
		}
		else
		{
			TimeShow();
			OLED_ShowNum(1, 7, DS1302_Time[7], 2);
			OLED_ShowNum(1, 10, DS1302_Time[8], 2);
			OLED_ShowNum(1, 13, DS1302_Time[9], 2);
		}
		
		if(DS1302_Time[3] == DS1302_Time[7] && DS1302_Time[4] == DS1302_Time[8] && DS1302_Time[5] == DS1302_Time[9])
		{
			Buzzer_Set(0);
			Serial_TxFlag = 1;
		}
		
		if (Serial_GetRxFlag() == 1)			//检查串口接收数据的标志位
		{
			Serial_RxData = Serial_GetRxData();		//获取串口接收的数据
			Serial_SendByte(Serial_RxData);			//串口将收到的数据回传回去，用于测试
		}
		if(((KeyNum == 1) && LightSensor_Get()) || Serial_RxData == 0xA5)//按键加光敏电阻 或 上位机发送开箱密码A5即可关闭蜂鸣器，打开药箱
		{
			Buzzer_Set(1);
			Serial_TxFlag = 0;
			Servo_Angle = 90;
			Serial_RxData = 0x00;
		}	
		if(KeyNum == 2)
		{
			Buzzer_Set(1);
			Servo_Angle = 0;
		}
			
		Servo_SetAngle(Servo_Angle);
		Motor_SetSpeed(Speed);	
	}
}
